package Platform.WandaProjectAuth;

import Base.DataFactory;
import Base.TestBase;
import org.apache.commons.lang.StringUtils;
import org.testng.Assert;

/**
 * Created by Alain on 2016/3/9.
 */
@SuppressWarnings("unused")
public class ProjectAuth extends TestBase {
    private static TestBase TB = new TestBase();
    private static DataFactory DF = new DataFactory();
    private String requestURL=null, response=null;
    private static String name  = DF.getCellValue("WandaProjectAuth", "name"),
                    roleId      = DF.getCellValue("WandaProjectAuth", "roleId"),
                    orgId       = DF.getCellValue("WandaProjectAuth", "orgId"),
                    userId      = DF.getCellValue("WandaProjectAuth", "userId"),
                    parentId    = DF.getCellValue("WandaProjectAuth", "parentId"),
                    type        = DF.getCellValue("WandaProjectAuth", "type"),
                    pageIndex   = DF.getCellValue("WandaProjectAuth", "pageIndex"),
                    functionId  = DF.getCellValue("WandaProjectAuth", "functionId"),
                    privilegeId = DF.getCellValue("WandaProjectAuth", "privilegeId");

    /*01.新增角色
    POST  https://bim.wanda.cn/platform/role?name={name}
    参数名         是否必选       类型      描述
    name        	必须	      string	角色名称*/
    public String createRole(){
        requestURL = "/platform/role?name="+name;

        //send request
        try {
            response = TB.apiResponse(requestURL, "POST");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*02.获取角色列表
    GET  https://bim.wanda.cn/platform/role
    参数名         是否必选       类型      描述
    */
    public String getRoleList(){
        requestURL = "/platform/role";

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*03.赋予指定角色功能权限
    POST  https://bim.wanda.cn/platform/role/{roleId}/function?functionId=111,222,333
    参数名         是否必选      类型      描述
    roleId      	是	         long	   角色Id*/
    public String setRolePrivilege(){
        requestURL = "/platform/role/"+roleId+"/function?functionId="+functionId;

        //send request
        try {
            response = TB.apiResponse(requestURL, "POST");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*04.获取指定角色的功能权限
    GET  https://bim.wanda.cn/platform/role/{roleId}/function
    参数名         是否必选      类型      描述
    roleId      	是	         long	   角色Id*/
    public String getRolePrivilege(){
        requestURL = "/platform/role/"+roleId+"/function";

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*05.为用户赋予角色
    POST  https://bim.wanda.cn/platform/user/role
    参数名         是否必选      类型      描述*/
    public String setUserRole(){
        requestURL = "/platform/user/role";
        String requestBody = "{\"userId\": [1,2,3,4,5],\"roleId\": [1]}";
        //send request
        try {
            response = TB.apiResponse(requestURL, "POST", requestBody);
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*06.为指定机构赋予角色
    POST  https://bim.wanda.cn/platform/org/{orgId}/role
    参数名         是否必选      类型      描述
    orgId       	是	         long	   机构Id*/
    public String setOrgRole(){
        requestURL = "/platform/org/"+orgId+"/role";

        //send request
        try {
            response = TB.apiResponse(requestURL, "POST");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*07.赋予指定机构数据权限
    POST  https://bim.wanda.cn/platform/org/{orgId}/dataPrivilege?privilegeId=500,501,502
    参数名         是否必选      类型      描述
    orgId       	是	         long	   机构Id*/
    public String setOrgDataPrivilege(){
        requestURL = "/platform/org/"+orgId+"dataPrivilege?privilegeId="+privilegeId;

        //send request
        try {
            response = TB.apiResponse(requestURL, "POST");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*08.获取指定用户的角色列表
    GET  https://bim.wanda.cn/platform/user/{userId}/role
    参数名         是否必选      类型        描述
    userId      	是	         long        用户Id*/
    public String getUserRoleList(){
        requestURL = "/platform/user/"+userId+"/role";

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*09.获取指定机构的角色列表
    GET  https://bim.wanda.cn/platform/org/{orgId}/role
    参数名         是否必选      类型        描述
    orgId      	    是	         long        机构Id*/
    public String getOrgRoleList(){
        requestURL = "/platform/org/"+orgId+"/role";

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*10.获取指定机构的数据权限
    GET  https://bim.wanda.cn/platform/org/{orgId}/dataPrivilege
    参数名         是否必选      类型        描述
    orgId      	    是	         long        机构Id*/
    public String getOrgDataPrivilege(){
        requestURL = "/platform/org/"+orgId+"/dataPrivilege";

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*11.获取指定用户的数据权限
    GET  https://bim.wanda.cn/platform/user/{userId}/dataPrivilege
    参数名         是否必选      类型        描述
    userId      	是	         long        用户Id*/
    public String getUserDataPrivilege(){
        requestURL = "/platform/user/"+userId+"/dataPrivilege";

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*12.删除角色
    DELETE  https://bim.wanda.cn/platform/role?roleId=100,200
    参数名         是否必选      类型          描述
    roleId      	是	         String        角色Id*/
    public String deleteRole(){
        requestURL = "/platform/role?roleId="+roleId;

        //send request
        try {
            response = TB.apiResponse(requestURL, "DELETE");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*13.删除指定角色的功能权限
    DELETE  https://bim.wanda.cn/platform/role/{roleId}/function?functionId=111,222
    参数名         是否必选      类型          描述
    roleId      	是	         String        角色Id
    functionId	    是	         String	       以逗号分隔的功能Id*/
    public String deleteRolePrivilege(){
        requestURL = "/platform/role/"+roleId+"/function?functionId="+functionId;

        //send request
        try {
            response = TB.apiResponse(requestURL, "DELETE");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*14.删除指定用户的角色
    DELETE  https://bim.wanda.cn/platform/user/role?roleId=111,222
    参数名         是否必选      类型          描述
    roleId      	是	         String        角色Id
    userId      	是	         long          用户Id*/
    public String deleteUserRole(){
        requestURL = "/platform/user/role?roleId="+roleId;

        //send request
        try {
            response = TB.apiResponse(requestURL, "DELETE");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*15.删除指定机构的角色
    POST  https://bim.wanda.cn/platform/org/{orgId}/role?roleId=999,888
    参数名         是否必选      类型          描述
    roleId      	是	         String        角色Id
    orgId      	    是	         long          机构Id*/
    public String deleteOrgRole(){
        requestURL = "/platform/org/"+orgId+"/role?roleId="+roleId;

        //send request
        try {
            response = TB.apiResponse(requestURL, "DELETE");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*16.删除指定机构的数据权限
    DELETE  https://bim.wanda.cn/platform/org/{orgId}/dataPrivilege?privilegeId=500,501
    参数名         是否必选      类型          描述
    privilege	    是	         string	       以逗号分隔的数据权限Id
    orgId      	    是	         long          机构Id*/
    public String deleteOrgDataPrivilege(){
        requestURL = "/platform/org/"+orgId+"/dataPrivilege?privilegeId="+privilegeId;

        //send request
        try {
            response = TB.apiResponse(requestURL, "DELETE");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*17.赋予指定用户数据权限
    POST  https://bim.wanda.cn/platform/user/dataPrivilege?userId=100,101,102&privilegeId=500,501,502
    参数名         是否必选      类型          描述
    privilege	    是	         string	       以逗号分隔的数据权限Id
    userId      	是	         long	       以逗号分隔的用户Id*/
    public String setUserDataPrivilege(){
        requestURL = "/platform/user/dataPrivilege?userId="+userId+"&privilegeId="+privilegeId;

        //send request
        try {
            response = TB.apiResponse(requestURL, "POST");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*18.删除指定用户数据权限
    DELETE  https://bim.wanda.cn/platform/user/{userId}/dataPrivilege?privilegeId=500,501
    参数名         是否必选      类型          描述
    privilege	    是	         string	       以逗号分隔的数据权限Id
    userId      	是	         long	       用户Id*/
    public String deleteUserDataPrivilege(){
        requestURL = "/platform/user/"+userId+"/dataPrivilege?privilegeId="+privilegeId;

        //send request
        try {
            response = TB.apiResponse(requestURL, "DELETE");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*19.获取机构列表
    GET  https://bim.wanda.cn/platform/org?parentId={parentId}
    参数名         是否必选      类型          描述
    parentId    	否         	 long	       父部门ID，为null则查询一级机构*/
    public String getOrgList(){
        requestURL = "/platform/org?";
        if (StringUtils.isNotBlank(parentId)){
            requestURL = TB.getURL(requestURL) + "parentId=" + parentId;
        }

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*20.获取用户列表
    GET  https://bim.wanda.cn/platform/user?orgId={orgId}&roleId={roleId}
    参数名         是否必选      类型          描述
    orgId       	否	         long	       所属机构Id，与roleId不可同时传入
    roleId	        否	         long	       角色Id，与orgId不可同时传入
    pageIndex	    否	         int	       第几页，默认为第1页
    name	        否	         string	       用户名称*/
    public void getUserList(){
        //request orgId
        requestURL = "/platform/user?";

        if (StringUtils.isNotBlank(orgId))
            requestURL = TB.getURL(requestURL) + "orgId=" + orgId;
        if (StringUtils.isNotBlank(pageIndex))
            requestURL = TB.getURL(requestURL) + "pageIndex=" + pageIndex;
        if (StringUtils.isNotBlank(name))
            requestURL = TB.getURL(requestURL) + "name=" + name;

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }


        //request roleId
        requestURL = "/platform/user?";

        if (StringUtils.isNotBlank(roleId))
            requestURL = TB.getURL(requestURL) + "roleId=" + roleId;
        if (StringUtils.isNotBlank(pageIndex))
            requestURL = TB.getURL(requestURL) + "pageIndex=" + pageIndex;
        if (StringUtils.isNotBlank(name))
            requestURL = TB.getURL(requestURL) + "name=" + name;

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }
    }

    /*21.获取数据权限列表
    GET  https://bim.wanda.cn/platform/dataPrivilege?type={type}&pageIndex={pageIndex}
    参数名         是否必选      类型          描述
    type	        否	         long	       数据权限类型,1:项目
    pageIndex	    否	         int	       第几页，默认为第1页*/
    public String getDataPrivilegeList(){
        requestURL = "/platform/dataPrivilege?";
        if (StringUtils.isNotBlank(type))
            requestURL = TB.getURL(requestURL) + "type=" + type;
        if (StringUtils.isNotBlank(pageIndex))
            requestURL = TB.getURL(requestURL) + "pageIndex=" + pageIndex;

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*22.获取功能权限列表
    GET  https://bim.wanda.cn/platform/function
    参数名         是否必选      类型          描述
    */
    public String getFunctionPrivilegeList(){
        requestURL = "/platform/function";

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*23.获取当前用户的角色列表
    GET  https://bim.wanda.cn/platform/user/role
    参数名         是否必选      类型          描述
    */
    public String getCurrentUserRoleList(){
        requestURL = "/platform/user/role";

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*24.获取当前用户的数据权限
    GET  https://bim.wanda.cn/platform/user/dataPrivilege
    参数名         是否必选      类型          描述
    */
    public String getCurrentUserDataPrivilege(){
        requestURL = "/platform/user/dataPrivilege";

        //send request
        try {
            response = TB.apiResponse(requestURL, "GET");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }

    /*25.修改角色
    PUT  https://bim.wanda.cn/platform/role?name={name}
    参数名         是否必选      类型          描述
    name        	是	         string        角色名称*/
    public String modifyUserRole(){
        requestURL = "/platform/role?name="+name;

        //send request
        try {
            response = TB.apiResponse(requestURL, "PUT");
        } catch (Exception e){
            Assert.fail("");
        }
        return response;
    }
}
